package com.example.exception;

import lombok.Data;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.http.HttpStatus;

/**
 * @author hello
 * @date 2023-04-12 15:54:11
 */
@Data
public class BusinessException extends RuntimeException {
    /**
     * 系统错误
     */
    public static final int ERROR = 500;

    private int code;

    public BusinessException(int code, String msg) {
        super(msg);
        this.code = code;
    }

    public BusinessException(String msg) {
        super(msg);
        this.code = ERROR;
    }

    public BusinessException(int code, String format, Object... arguments) {
        super(MessageFormatter.arrayFormat(format, arguments).getMessage());
        this.code = code;
    }

    public BusinessException(String format, Object... arguments) {
        super(MessageFormatter.arrayFormat(format, arguments).getMessage());
        this.code = ERROR;
    }

    public static void throwable(int code, String format, Object... arguments) {
        throw new BusinessException(code, format, arguments);
    }

    public static void throwable(HttpStatus httpStatus, String format, Object... arguments) {
        throw new BusinessException(httpStatus.value(), format, arguments);
    }

    public static void throwable(String format, Object... arguments) {
        throw new BusinessException(ERROR, format, arguments);
    }
}
